package com.zh.blog.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.codec.binary.Hex;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;

/**
 * 文件工具类
 * @author ZhengHao
 * Created At 2022/8/4.
 **/
@Log4j2
public class FileUtils {

    /**
     * 获取文件MD5值
     * @param inputStream 文件输入流
     * @return 文件MD5值
     */
    public static String getMD5(InputStream inputStream){
        try {
            MessageDigest md5 = MessageDigest.getInstance("md5");
            byte[] buffer = new byte[8192];
            int length;
            while ((length = inputStream.read(buffer)) != -1){
                md5.update(buffer, 0, length);
            }
            return new String(Hex.encodeHex(md5.digest()));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (inputStream != null){
                    inputStream.close();
                }
            } catch (IOException e){
                e.printStackTrace();
            }
        }

    }

    /**
     * 得到文件扩展名
     * @param filename 文件名称
     * @return 文件后缀
     */
    public static String getExtName(String filename) {
        if (StringUtils.isBlank(filename)){
            return "";
        }
        return filename.substring(filename.lastIndexOf("."));
    }
}